﻿using System;

namespace core
{
    /// <summary>Trieda ICompressionDataStr</summary>
	public class ICompressionDataStr
	{
        public const int COMPRESSION_TYPE_NONE = 0;
        public const int COMPRESSION_TYPE_GZIP = 1;
        public const int COMPRESSION_TYPE_TAR = 2;
        public const int COMPRESSION_TYPE_TGZ = 3;
        public const int COMPRESSION_TYPE_ZIP = 4;

        public int CompressionType; // Typ komprimacie
        public string DestinationArchiveName; // Nazov cieloveho archivu
        public int ZIPCompression_Level; // Level komprimacie ZIP
        public int ZIPEncryptionType; // Typ sifrovania
        public string ZIPPassword; // Heslo na sifrovanie

        /// <summary>Konstruktor</summary>
        public ICompressionDataStr()
        {
            CompressionType = COMPRESSION_TYPE_NONE;
            ZIPCompression_Level = 5;
            ZIPEncryptionType = 0;
            ZIPPassword = "";
        }

        /// <summary>Skopirovanie obsahu objektu</summary>
        /// <param name="pValue">objekt</param>
        public void Assign(ICompressionDataStr pValue)
        {
            CompressionType = pValue.CompressionType;
            DestinationArchiveName = pValue.DestinationArchiveName;
            ZIPCompression_Level = pValue.ZIPCompression_Level;
            ZIPEncryptionType = pValue.ZIPEncryptionType;
            ZIPPassword = pValue.ZIPPassword;
        }
        /// <summary>Serializacia obsahu</summary>
        /// <returns>serializovany obsah</returns>
        public string Serialize()
        {
            using (IParamList L = new IParamList())
            {
                L.SetParam("COMPRESSION_TYPE", CompressionType);
                L.SetParam("ZIP_COMPRESSION_LEVEL", ZIPCompression_Level);
                L.SetParam("ZIP_ENCRYPTION_TYPE", ZIPEncryptionType);
                L.SetParam("ZIP_PASSWORD", ZIPPassword);
                return L.Serialize(true);
            }
        }
        /// <summary>Deserializacia obsahu</summary>
        /// <param name="pData"></param>
        public void Deserialize(string pData)
        {
            using (IParamList L = new IParamList())
            {
                L.Deserialize(pData, true);
                CompressionType = L.GetParam("COMPRESSION_TYPE", CompressionType);
                ZIPCompression_Level = L.GetParam("ZIP_COMPRESSION_LEVEL", ZIPCompression_Level);
                ZIPEncryptionType = L.GetParam("ZIP_ENCRYPTION_TYPE", ZIPEncryptionType);
                ZIPPassword = L.GetParam("ZIP_PASSWORD", ZIPPassword);
            }
        }

        /// <summary>Nastavenie parametrov komprimacie pomocou formulara</summary>
        /// <returns>true / false</returns>
        public bool EditWithForm()
        {
            switch (CompressionType)
            {
                case COMPRESSION_TYPE_ZIP: // ZIP komprimacia
                    {
                        using (IFileExplorerForm_EditZIPCompressionParams F = new IFileExplorerForm_EditZIPCompressionParams())
                        {
                            F.Value.Assign(this);
                            bool B = F.ShowDialog() == System.Windows.Forms.DialogResult.OK;
                            if (B) Assign(F.Value);
                            return B;
                        }
                    }
                default: return true; // Nic sa needituje
            }
        }

        /// <summary>Ziskanie prednastavenej pripony archivu</summary>
        /// <param name="pCompressionType">typ kompresie</param>
        /// <returns>pripona alebo prazdny retazec</returns>
        public static string GetDefaultExtension(int pCompressionType)
        {
            switch (pCompressionType)
            {
                case COMPRESSION_TYPE_GZIP: return "gz";
                case COMPRESSION_TYPE_TAR: return "tar";
                case COMPRESSION_TYPE_TGZ: return "tgz";
                case COMPRESSION_TYPE_ZIP: return "zip";
                default: return "";
            }
        }
	}

    /// <summary>Trieda IFileExplorerForm_EditZIPCompressionParams</summary>
    public class IFileExplorerForm_EditZIPCompressionParams : IForm
    {
        public ICompressionDataStr Value; // Objekt pre zmenu

        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_OK;
        protected IButton FBtn_Cancel;

        protected ILabel FLabel1, FLabel2, FLabel3;
        protected ITrackBar FTrack_Level;
        protected ILabel FLabel4;
        protected IComboBox FCombo_EncryptionType;
        protected ILabel FLabel5;
        protected ITextBox FText_Password;

        /// <summary>Konstruktor</summary>
        public IFileExplorerForm_EditZIPCompressionParams()
        {
            Value = new ICompressionDataStr();
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Inicializacia
            MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = false; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetBounds(0, 0, 488, 260); System.Drawing.Size CS = ClientSize;
            Text = lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_1;

            // Vytvorim groupbox
            FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);

            FLabel1 = new ILabel(lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_4); FLabel2 = new ILabel("0"); FLabel3 = new ILabel("9");
            FTrack_Level = new ITrackBar(); FTrack_Level.Minimum = 0; FTrack_Level.Maximum = 9;
            FLabel4 = new ILabel(lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_5);
            FCombo_EncryptionType = new IComboBox(); FCombo_EncryptionType.AddMultiItems(lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_6, '\b', new int[] { 0, 1, 2, 3 });
            FLabel5 = new ILabel(lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_7);
            FText_Password = new ITextBox(); FText_Password.PasswordChar = '*';

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel1, FTrack_Level, FLabel2, FLabel3, FLabel4, FCombo_EncryptionType, FLabel5, FText_Password });

            IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel1, FGrp, 10, 20);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FTrack_Level, FLabel1, 0, 10, FGrp.ClientSize.Width - 20);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel2, FTrack_Level, 7, -16); FLabel2.BringToFront();
            IControl.PlaceControl_Outside_TopRight_RightBottom(FLabel3, FTrack_Level, 6, -16); FLabel3.BringToFront();
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel4, FLabel2, 0, 20);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FCombo_EncryptionType, FLabel4, 2, 10, FGrp.ClientSize.Width / 2 - 20);
            IControl.PlaceControl_Outside_LeftTop_LeftTop(FLabel5, FLabel4, FGrp.ClientSize.Width / 2, 0);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_Password, FLabel5, 2, 11, FGrp.ClientSize.Width / 2 - 30);

            // Vytvorim tlacidla
            FBtn_Cancel = new IButton(lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_3); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_EDIT_ZIP_COMPRESSION_PARAMS_TEXT_2, Event_BtnOKClick); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);

            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus();

            FCombo_EncryptionType.SelectedIndexChanged += (s, e) => // Event
            {
                UpdateForm();
            };

            FillForm(Value); // Vyplnim zaciatocnou hodnotou
            return true; // Vsetko OK
        }

        /// <summary>Aktualizacia stavu formulara</summary>
        protected void UpdateForm()
        {
            FText_Password.Enabled = FCombo_EncryptionType.SelectedIntID > 0;
        }
        /// <summary>Vyplnenie formulara</summary>
        /// <param name="pValue">hodnota</param>
        protected void FillForm(ICompressionDataStr pValue)
        {
            FTrack_Level.Value = pValue.ZIPCompression_Level;
            FCombo_EncryptionType.SelectedIntID = pValue.ZIPEncryptionType;
            FText_Password.Text = pValue.ZIPPassword;
            UpdateForm();
        }
        /// <summary>Nacitanie obsahu formulara</summary>
        /// <param name="Result">vysledok</param>
        protected void ReadForm(ICompressionDataStr Result)
        {
            Result.ZIPCompression_Level = FTrack_Level.Value;
            Result.ZIPEncryptionType = FCombo_EncryptionType.SelectedIntID;
            if (Result.ZIPEncryptionType > 0) Result.ZIPPassword = FText_Password.Text;
        }

        /// <summary>Event: OnClick pre tlacidlo FBtn_OK</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnOKClick(object pSender, EventArgs e)
        {
            ReadForm(Value); // Nacitam obsah
            DialogResult = System.Windows.Forms.DialogResult.OK; // Ukoncim dialog
        }
    }
}
